package com.gitee.tje.waving.factory;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 名字映射关系
 *
 * @author taojiaen
 * @date 2018-01-17 08:06
 */
public class NamedBeanMap {
    private Map<String, Object> beanNameMap = new ConcurrentHashMap<>();


    public  void addBean(String name, Object bean) {
        beanNameMap.put(name, bean);
    }

    public Object getBean(String name) {
       return  beanNameMap.get(name);
    }
    public boolean contains(String name) {
        return beanNameMap.containsKey(name);
    }
    public<T> Collection<T> getBeans(Class<T> clazz) {
        if (clazz == null) {
            throw new NullPointerException("clazz is null");
        }


         Set<T> hashSet = new HashSet<>();
        for (Object object : beanNameMap.values()) {
            if (clazz.isAssignableFrom(object.getClass())){
                hashSet.add((T) object);
            }
        }
        return hashSet;
    }
    public void clear() {
        beanNameMap.clear();
    }
}
